package com.chanjiedata.changbs.module.count;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.base.BaseFragment;
import com.google.android.material.tabs.TabLayout;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/2/2 0002.
 */

public class CountFragment extends BaseFragment {

    private Unbinder unbinder;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    public ViewPager viewPager;

    private String[] titles = {"收益", "交易", "激活", "发展", "台均", "商户", "伙伴"};
    private List<Fragment> fragmentList;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_count;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected void initView() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new CountFirstFragment());
        fragmentList.add(new CountSecondFragment());
        fragmentList.add(new CountThirdFragment());
        fragmentList.add(new CountFourthFragment());
        fragmentList.add(new CountFifthFragment());
        fragmentList.add(new CountSixthFragment());
        fragmentList.add(new CountSeventhFragment());
        viewPager.setOffscreenPageLimit(7);
        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }

        });
        initTabLayout();
    }


    @Override
    protected void init() {
    }


    //设置tablayout
    private void initTabLayout() {
        tabLayout.setupWithViewPager(viewPager);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        ((CountFirstFragment) (fragmentList.get(0))).reLoadData();
                        break;
                    case 1:
                        ((CountSecondFragment) (fragmentList.get(1))).reLoadData();
                        break;
                    case 2:
                        ((CountThirdFragment) (fragmentList.get(2))).reLoadData();
                        break;
                    case 3:
                        ((CountFourthFragment) (fragmentList.get(3))).reLoadData();
                        break;
                    case 4:
                        ((CountFifthFragment) (fragmentList.get(4))).reLoadData();
                        break;
                    case 5:
                        ((CountSixthFragment) (fragmentList.get(5))).reLoadData();
                        break;
                    case 6:
                        ((CountSeventhFragment) (fragmentList.get(6))).reLoadData();
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab == null || tab.getText() == null) {
                    return;
                }
                scaleAnima(1.0f, 1.3f, tab.view);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab.view == null || tab.getText() == null) {
                    return;
                }
                scaleAnima(1.3f, 1.0f, tab.view);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        scaleAnima(1.0f, 1.3f, tabLayout.getTabAt(0).view);

    }


    private void scaleAnima(float v, float v2, TabLayout.TabView title) {
        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = new ScaleAnimation(
                v, v2, v, v2,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(100);//动画持续时间
        animationSet.addAnimation(scaleAnimation);//保存动画效果到。。
        animationSet.setFillAfter(true);//结束后保存状态
        title.startAnimation(animationSet);//设置给控件
    }


}
